#!/usr/bin/env python
#coding:utf-8
#一个像sed一样调用回调来替换的例子

import re

filelines='''chinaunix.a1 ,
chinaunix.ay1        ,
chinaunix.ad2        ,
chinaunix.a3o        ,
chinaunix.a4'''

def main(filelines):
    restr=r'(\w+)\.(\w+)(.*)'
    replstr=r'\2.\1\3'
    for it in filelines.split('\n'):
        print 'before replace:',it
        ret=re.sub(restr,replstr,it)
        print 'after replace:',ret
        print '='*50

if __name__ == '__main__':
    main(filelines)
    print 'done'
    